package com.atguigu.flink.streamapi.transform;

import com.atguigu.flink.pojo.WaterSensor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * Created by Smexy on 2022/11/21
 *
 *  先keyBy，才能reduce
 *
 *      maxBy:  求最大值
 *      minBy:  求最小值
 *
 *      返回聚合的字段最大值的所有属性。
 *
 *      maxBy("vc") 等价于 maxBy("vc", true)
 *              true: 意味着两条数据都是最大的，是否取第一条(最早那条)
 */
public class Demo9_AggBy
{
    public static void main(String[] args) {


        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env
           .socketTextStream("hadoop103", 8888)
           .map(new MapFunction<String, WaterSensor>()
           {
               @Override
               public WaterSensor map(String value) throws Exception {
                   String[] data = value.split(",");
                   return new WaterSensor(
                       data[0],
                       Long.valueOf(data[1]),
                       Integer.valueOf(data[2])
                   );
               }
           })
           .keyBy(w -> "a")
           //.maxBy("vc")  //求最大水位,两条都最大，取第一条
           .maxBy("vc",false)  //求最大水位,两条都最大，取最后一条
           .print();

        try {
                    env.execute();
                } catch (Exception e) {
                    e.printStackTrace();
                }

    }
}
